﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using pTools.cMethods;
using Entity.DbEntity;
using Newtonsoft.Json;
using pTools.PrintTable;
using Newtonsoft.Json.Linq;

namespace pTools.TuQiangPrint
{

    //    打印个数
    //数据都从条码系统获取
    //简单的选择，做成单选框
    public partial class EquipmentPrint : DevExpress.XtraEditors.XtraForm
    {
        U8DbHelper u8Db = new U8DbHelper();
        DBHelper db = new DBHelper();
        HttpAgreement http = new HttpAgreement();
        List<DbSheBeiBar> models = new List<DbSheBeiBar>();

        // 采购订单列表
        //select * from PO_Pomain main left join PO_Podetails detail on main.POID = detail.ID

        public EquipmentPrint()
        {
            InitializeComponent();

            grid_printList.DataSource = models;
        }

        private void TQPurchasePrint_Load(object sender, EventArgs e)
        {
            // 加载打印机和模板
            LoadPrinterInfo();

            //加载机器类型
            CommonMethods.loadDictionaries("sbType", cb_types);
        }

        void LoadPrinterInfo()
        {
            // 加载模板
            CommonMethods.loadPrintTemplte(cb_0_Template);
            // 加载打印机
            CommonMethods.loadPrintDevice(cb_0_Print);
        }

        //选择档案
        private void btn_select_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(cb_types.Text))
            {
                models.Clear();

                SelectSBInfo sb = new SelectSBInfo();

                sb.type = cb_types.SelectedValue.ToString();
                sb.ShowDialog();

                if (sb.selected.Rows.Count == 0)
                {
                    return;
                }

                var table = sb.selected;

                foreach (DataRow dr in table.Rows)
                {
                    var im = new DbSheBeiBar
                    {
                        happenDate = DateTime.Now.ToString("yyyy-MM-dd"),
                        createtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        createuser = CommonMethods.userCode,
                        sbCode = dr["scode"].ToString(),
                        sbName = dr["sname"].ToString(),
                        barStatus = "open",
                        status = "Y",
                        barType = cb_types.SelectedValue.ToString(),
                        barTypeName = cb_types.Text.ToString(),
                    };
                    models.Add(im);
                }

                grid_printList.RefreshDataSource();
            }
        }

        private void btn_beginPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            PrintsbBarcode();
        }

        void PrintsbBarcode()
        {
            if (models.Count < 0)
            {
                MessageBox.Show("未选择打印列表");
                return;
            }
            // 开始组织数据
            List<DbSheBeiBar> list = new List<DbSheBeiBar>();
            foreach (var s in models)
            {
                list.Add(s);
            }

            var jsonResult = http.PostJson("SaveSBBarCode", JsonConvert.SerializeObject(list));

            var rsl = JsonConvert.DeserializeObject<ReturnResult>(jsonResult);

            if (rsl.Result)
            {
                #region 打印代码
                var bars = rsl.Data.ToString().Split(',');
                DataTable table;
                string info = "";
                table = CommonMethods.onlyBarCode();

                foreach (string s in bars)
                {
                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        var r = table.NewRow();
                        r["barCode"] = s;
                        table.Rows.Add(r);
                    }
                }

                var pResult = CommonMethods.DoPrint(table, cb_0_Template.Text, cb_0_Print.Text, ref info);
                if (!pResult)
                {
                    MessageBox.Show(info, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                #endregion

            }
            else
            {
                MessageBox.Show(rsl.Message);
            }
        }

        public static string SerializeObject<T>(T value, JsonSerializerSettings settings)
        {
            if (value == null)
            {
                return null;
            }

            var dictionaryObject = new Dictionary<string, object> { { typeof(T).Name, value } };
            var jsonString = JsonConvert.SerializeObject(dictionaryObject, settings);

            return jsonString;
        }

        public static T DeserializeObject<T>(string jsonString, JsonSerializerSettings settings)
        {
            var objectValue = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString, settings);
            return JsonConvert.DeserializeObject<T>(objectValue.Values.First().ToString(), settings);
        }









        private void btn_quit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //强制结束进程并退出
            System.Diagnostics.Process.GetCurrentProcess().Kill();
            System.Environment.Exit(0);

            Application.Exit();
        }

        
    }

}